package com.wnhz.work.yz.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ProgressFindBean;
import com.wnhz.work.yz.bean.ServiceProgressBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class ServiceProcessActivity extends Activity {
    private TextView tv_good_code, tv_time;
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_process);
        initData();
        initTitle();
        initView();
    }

    private void initView() {
        tv_good_code = (TextView) findViewById(R.id.tv_good_code);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_good_code.setText(find_info.getOrder_id());
        tv_time.setText(find_info.getAdd_time());
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("进度查询");
        findViewById(R.id.btn_main_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private ProgressFindBean.InfoEntity find_info = new ProgressFindBean.InfoEntity();
    private String serviceId;

    private void initData() {
        find_info = (ProgressFindBean.InfoEntity) getIntent().getSerializableExtra("infoItem");
        serviceId = getIntent().getStringExtra("serviceId");
        upLoad();
    }

    private List<ServiceProgressBean.InfoEntity> progress_bean = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("serviceid", serviceId);
        XUtil.Post(Url.ORDER_SERVICEPROCESS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                ServiceProgressBean bean = gson.fromJson(result, ServiceProgressBean.class);
                progress_bean = bean.getInfo();
            }

            @Override
            public void onFinished() {
                super.onFinished();
                setData();
            }

        });
    }

    private void setData() {
        recycler.scrollToPosition(0);

        recycler.setAdapter(new BaseRVAdapter(this, progress_bean) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.progress_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                if (position != 0) {
                    holder.getImageView(R.id.progress_img).setBackgroundResource(R.drawable.progress_gray);
                    holder.getTextView(R.id.progress_content).setTextColor(getResources().getColor(R.color.text));
                    holder.getTextView(R.id.progress_time).setTextColor(getResources().getColor(R.color.text));
                } else {
                    holder.getImageView(R.id.progress_img).setBackgroundResource(R.drawable.progress_green);
                }
                holder.setTextView(R.id.progress_content, progress_bean.get(position).getContent());
                holder.setTextView(R.id.progress_time, progress_bean.get(position).getCreat_time());

            }

        });
    }

}
